package com.whiteape.book.model.bo;

import com.whiteape.book.model.type.RuleType;
import com.whiteape.book.utils.StringUtils;

public class RuleBo {
    protected RuleType ruleType;
    protected String content;

    public RuleBo(){}
    public RuleBo(String content){this.content = content;}
    public RuleBo(RuleType ruleType, String content) {
        this.ruleType = ruleType;
        String upperContent = content.toUpperCase();
        if (upperContent.startsWith(RuleType.CSS.getName()) ||
                upperContent.startsWith(RuleType.XPATH.getName()) ||
                upperContent.startsWith(RuleType.JSON.getName()) ||
                upperContent.startsWith(RuleType.JS.getName()) ||
        upperContent.startsWith(RuleType.STR.getName())) {
            this.content = content.substring(ruleType.getName().length());
        } else {
            this.content = content;
        }
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        if (ruleType == null) {
            return;
        }
        String upperContent = content.toUpperCase();
        if (upperContent.startsWith(RuleType.CSS.getName()) ||
                upperContent.startsWith(RuleType.XPATH.getName()) ||
                upperContent.startsWith(RuleType.JSON.getName()) ||
                upperContent.startsWith(RuleType.JS.getName()) ||
                upperContent.startsWith(RuleType.STR.getName())) {
            this.content = content.substring(ruleType.getName().length());
        } else {
            this.content = content;
        }

//        this.content = content;
    }

    public RuleType getRuleType() {
        return ruleType;
    }

    public void setRuleType(RuleType ruleType) {
        this.ruleType = ruleType;
        if (StringUtils.isEmpty(content) ) {
            return;
        }

        String upperContent = content.toUpperCase();
        if (upperContent.startsWith(RuleType.CSS.getName()) ||
                upperContent.startsWith(RuleType.XPATH.getName()) ||
                upperContent.startsWith(RuleType.JSON.getName()) ||
                upperContent.startsWith(RuleType.JS.getName()) ||
                upperContent.startsWith(RuleType.STR.getName())) {
            this.content = content.substring(ruleType.getName().length());
        }
    }

    @Override
    public String toString() {
        return "RuleBo{" +
                "ruleType=" + ruleType +
                ", content='" + content + '\'' +
                '}';
    }
}
